﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using IdentityMine.Windows.Eventing;

namespace SilverlightEventing.IdentityMine.Windows.Eventing
{
    public class RoutedEventHandlerDataCollection : List<RoutedEventHandlerData>
    {
        //private System.Collections.Generic.Dictionary<int, RoutedEventHandlerData> _quicklist = new System.Collections.Generic.Dictionary<int, RoutedEventHandlerData>();

        public RoutedEventHandlerData GetFor(Delegate handler)
        {
            RoutedEventHandlerData ret = null;

            foreach (RoutedEventHandlerData data in this)
            {
                if (data.Handler == handler)
                {
                    ret = data;
                    break;
                }
            }

            return ret;
        }

        public void RaiseAgainstElement(UIElement uielement, RoutedEventArgsEx args)
        {
            // TODO: Should make this faster...

            foreach (RoutedEventHandlerData data in this)
            {
                if (data.UIElement == uielement)
                {
                    args.Source = uielement;
                    data.InvokeEventHandler(uielement, args);
                }
            }

        }

        public void RaiseAgainstEvent(UIElement uielement, RoutedEventArgsEx args)
        {
            // TODO: Should make this faster...

            foreach (RoutedEventHandlerData data in this)
            {
                if (data.RoutedEvent == args.RoutedEvent)
                {
                    args.Source = uielement;
                    data.InvokeEventHandler(uielement, args);
                }
            }
        }

        public void AddEntry(RoutedEventHandlerData data)
        {
            //int hash = data.UIElement.GetHashCode();

            if (!this.Contains(data))
            {
                this.Add(data);
            }
            //if (!_quicklist.ContainsKey(hash))
            //{
            //    _quicklist.Add(hash, data);
            //}
        }

        public void RemoveEntry(RoutedEventHandlerData data)
        {
            //int hash = data.UIElement.GetHashCode();

            if (this.Contains(data))
            {
                this.Remove(data);
            }
            //if (_quicklist.ContainsKey(hash))
            //{
            //    _quicklist.Remove(hash);
            //}
        }
    }
}
